var express = require('express')
var router = express.Router()
var category = require('../models/category');
var Blog =require('../models/blog')
var config =require("../config")
var Favorites = require("../models/favorites")
var Links  = require('../models/links');

router.get('/',async function (req,res){
    let blogs= await Blog.find({}).limit(3).skip(0);
    res.render('./main/index',{
        blogs:blogs
    })
})
router.get('/link',async function (req,res){
    let links =await Links.find({}).limit(3).skip(0);
    res.render('./main/link',{
        user:req.user,
        links:links,
    })
})
router.get('/blog', async function (req, res) {
    let categorys =await category.find({});
    let name = req.query.name;
    let maxItem = config.pageSetting.MaxItem;
    let count = await  Blog.find({}).countDocuments();
    let maxPage = await Math.max(Math.ceil(count/maxItem),1);
    let temp=Math.max(req.query.page||1);
    let page = Math.min((temp||1),maxPage);
    let links = await Links.find({});
    if(name){
        let blogs = await Blog.find({category:name}).limit(maxItem).skip((page-1)*maxItem);
        res.render("./main/article",
            {user:req.user,
                categorys:categorys,
                blogs:blogs,
                page:page,
                maxPage:maxPage,
                Links:links
            })
    }else {
        let blogs = await Blog.find({}).limit(maxItem).skip((page-1)*maxItem);
        res.render("./main/article",
            {user:req.user,
                categorys:categorys,
                blogs:blogs,
                page:page,
                maxPage:maxPage,
                Links:links
            })
    }

})
router.post('/content',async function (req,res){
    let blogId=req.body.id;
    let blogContent;
     await Blog.findById(blogId,function (err,doc){
          blogContent=doc.content;
    })
    console.log("sss");
    res.send(blogContent);
})


//查看具体文章
router.get('/detail', async function (req, res) {
    let id = req.query.id;
    let hasPower =true;
    await Blog.findById(id,function (err,doc){
        if(req.user==null&&doc.category=='日记'){
            hasPower=false;
        } else if(doc.category=='日记'&&!req.user.isAdmin){
            hasPower=false;
        }
        let iniReading = doc.reading+1;
        Blog.updateOne({_id:id},{reading:iniReading},function (err){})

    })
    let categorys =await category.find({});
    let maxItem = config.pageSetting.MaxItem;
    let count = await  Blog.find({}).countDocuments();
    let maxPage = await Math.max(Math.ceil(count/maxItem),1);
    let temp=Math.max(req.query.page||1);
    let page = Math.min((temp||1),maxPage);
    let blog = await Blog.findOne({_id:id},function (err){
        if(err){
            console.log(err);
        }
    });
    if(hasPower){
        res.render("./main/read",
            {
                user:req.user,
                categorys:categorys,
                blog:blog,
                page:page,
                maxPage:maxPage
            })
    }else {
        res.send('不能偷看我的日记哦！Hi~ o(*￣▽￣*)ブ');
    }

})

//添加至收藏夹
router.post('/favorites/add', async function (req,res){
    let id = req.body.id.toString();
    let temp = Favorites.findOne({blogId:id});
    if(temp==[]){
        res.send({msg:"文章已收藏"})
    }else {
        new Favorites({blogId:id}).save();
        res.send({msg:"添加收藏成功"})
    }
})

router.post('/favorites/delete', async function (req,res){
    let id = req.body.id;
    await Favorites.deleteOne({blogId:id},function (err){if(err){
        console.log(err);}})
    res.send({msg:"删除成功"})
})

module.exports = router